package com.example.kehoobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.kehoobackend.domain.ChatRelation;
import com.example.kehoobackend.domain.Message;
import com.example.kehoobackend.domain.User;
import com.example.kehoobackend.persistance.ChatRelationMapper;
import com.example.kehoobackend.persistance.MessageMapper;
import com.example.kehoobackend.service.MessageService;
import com.example.kehoobackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("messageService")
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private ChatRelationMapper chatRelationMapper;

    @Autowired
    private UserService userService;


    @Override
    public boolean saveMessageRecord(Message message) {
        return save(message);
    }

    @Override
    public List<User> getChattedUsers(Integer user_id) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ChatRelation> chatRelationQueryWrapper = new QueryWrapper<>();

        List<Integer> ids = new ArrayList<>();
        List<User> resultList = new ArrayList<>();

        //当前用户ID为ChatRelation中的第一ID
        chatRelationQueryWrapper.select("second_id").eq("first_id",user_id);
        List<ChatRelation> second_ids = chatRelationMapper.selectList(chatRelationQueryWrapper);
        for(ChatRelation chatRelation : second_ids) ids.add(chatRelation.getSecond_id());

        //当前用户ID为ChatRelation中的第二ID
        chatRelationQueryWrapper.clear();
        chatRelationQueryWrapper.select("first_id").eq("second_id",user_id);
        List<ChatRelation> first_ids = chatRelationMapper.selectList(chatRelationQueryWrapper);
        for(ChatRelation chatRelation : first_ids){
            if(!ids.contains(chatRelation.getFirst_id()))
                ids.add(chatRelation.getFirst_id());
        }

        if(ids.isEmpty()){
            return resultList;
        }

        userQueryWrapper.in("user_id",ids).select("user_id","username");
        resultList = userService.list(userQueryWrapper);

        return resultList;

    }

    @Override
    public List<Message> getMessageRecord(Integer first_id,Integer second_id) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("src_id",first_id).eq("target_id",second_id)
                .or()
                .eq("src_id",second_id).eq("target_id",first_id);

        return list(queryWrapper);
    }

    @Override
    public boolean isChatted(Integer current_id, Integer target_id) {
        QueryWrapper<ChatRelation> chatRelationQueryWrapper = new QueryWrapper<>();
        chatRelationQueryWrapper.eq("first_id",current_id).eq("second_id",target_id)
                .or()
                .eq("first_id",target_id).eq("second_id",current_id);

        return chatRelationMapper.selectOne(chatRelationQueryWrapper) != null;
    }

    @Override
    public boolean addChatRelation(Integer first_id, Integer second_id) {
        ChatRelation chatRelation = new ChatRelation();
        chatRelation.setFirst_id(first_id);
        chatRelation.setSecond_id(second_id);
        return chatRelationMapper.insert(chatRelation) == 1;
    }
}
